package com.bkq.cb.BLO;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.bkq.cb.SqliteServices;
import com.bkq.cb.DTO.*;

/**
 * @author Administrator
 *
 */

public class UserBUO {

	SqliteServices services=null;
	static final String TAG="userdto";
	
	public UserBUO(Context context)
	{
		services=new SqliteServices(context);
	}
	
	
	
	public boolean checkIsUser(String userName,String passWord)
	{
		ArrayList<UserDTO> userAll=getAll("select * from tblUser");
		for(UserDTO v:userAll)
		{
			if( v.UserName.equals(userName)&& v.PassWord.equals(passWord))
			{
				return true;
			}
		}
		return false;
	}
	
	public int getID(String userName,String passWord)
	{
		ArrayList<UserDTO> userAll=getAll("select * from tblUser");
		for(UserDTO v:userAll)
		{
			if( v.UserName.equals(userName)&& v.PassWord.equals(passWord))
			{
				return v.ID;
			}
		}
		return -1;
	}
	
	/**
	 * get All user 
	 * @param query
	 *  Query string you need query
	 * @return
	 */
	public ArrayList<UserDTO> getAll(String query)
	{
		
		ArrayList<UserDTO> temp=new ArrayList<UserDTO>();
		
		UserDTO user;
		
		Cursor c=this.services.queryData(query);
		
		if(c!=null)
		{
			if(c.moveToFirst())
			{
			 do
			 {
				user=new UserDTO();
				
				
				user.UserName=c.getString(c.getColumnIndex("username"));
				user.PassWord=c.getString(c.getColumnIndex("password"));
				user.Name=c.getString(c.getColumnIndex("name"));
				
				temp.add(user);
				 
			 }while(c.moveToNext());
				
			}
		}
		
		return temp.size()==0? null:temp;
	}

	
	
	/**
	 * Insert user
	 * @param obj
	 * obj user
	 * @return
	 * true if susscess else false
	 */
	public boolean inser(UserDTO obj)
	{
		String sql="insert into tblUser(name,username,password) values('"+obj.Name +"','"+obj.UserName+"','"+obj.PassWord+"')";		
		boolean check=false;
		
		try
		{
			this.services.exeSQL(sql);
			check=true;
		}
		catch(Exception ex)
		{
			Log.d(TAG, "Insert user Error");
			
		}
		return check;
		
	}
	
	 

    
}
